package com.example.team9login;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    private EditText edtMainUser;
    private EditText edtMainPsw;
    private CheckBox cbMainRempsw;
    private CheckBox cbMainAutologin;
    private Button btnMainLogin;
    private Button btnMainRegister;
    private TextView tvMainSetting;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        cbMainAutologin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("chkName",b);
                editor.commit();
            }
        });
        cbMainRempsw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor=sp.edit();
                editor.putBoolean("chkPSW",b);
                editor.commit();
                if(b){
                    cbMainAutologin.setChecked(true);
                    cbMainAutologin.setEnabled(false);
                }else {
                    cbMainAutologin.setEnabled(true);
                }
            }
        });
        btnMainLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName=edtMainUser.getText().toString();
                String password=edtMainPsw.getText().toString();
                if(userName.equals("admin")&&password.equals("admin")) {
                    if (edtMainUser.isClickable()) {
                        SharedPreferences.Editor editor = sp.edit();//
                        editor.putString("userName", userName);
                        editor.commit();
                    }
                    Intent intent = new Intent(LoginActivity,this.class);
                    intent.putExtra("passWord", edtMainPsw.getText().toString().trim());
                    Bundle bundle = new Bundle();
                    bundle.putString("passWord", edtMainPsw.getText().toString().trim());
                    startActivity(intent);
                }else {
                    androidx.appcompat.app.AlertDialog.Builder builder=new androidx.appcompat.app.AlertDialog.Builder(MainActivity.this);
                    builder.setMessage("请输入的用户名或密码错误！");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            MainActivity.this.finish();
                        }
                    });
                    AlertDialog dialog =builder.create();
                    dialog.show();

                    if(cbMainAutologin.isChecked()){
                        SharedPreferences.Editor editor=sp.edit();//
                        editor.putString("password",password);
                        editor.commit();

                    }
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        boolean chkName = sp.getBoolean("chkName", false);
        cbMainAutologin.setChecked(chkName);
        boolean chkPSW = sp.getBoolean("chkPSW", false);
        cbMainRempsw.setChecked(chkPSW);

        if(chkName){
            edtMainUser.setText(sp.getString("userName",null));
        }
        if(chkPSW){
            edtMainPsw.setText(sp.getString("password",null));
        }
    }

    private void initView() {
        edtMainUser = (EditText) findViewById(R.id.edt_main_user);
        edtMainPsw = (EditText) findViewById(R.id.edt_main_psw);
        cbMainRempsw = (CheckBox) findViewById(R.id.cb_main_rempsw);
        cbMainAutologin = (CheckBox) findViewById(R.id.cb_main_autologin);
        btnMainLogin = (Button) findViewById(R.id.btn_main_login);
        btnMainRegister = (Button) findViewById(R.id.btn_main_register);
        tvMainSetting = (TextView) findViewById(R.id.tv_main_setting);
    }
}
